package com.csp.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csp.common.utils.R;
import com.csp.main.entity.Commodity;
import com.csp.main.entity.vo.CommodityVo;
import com.csp.main.service.CommodityService;
import com.csp.main.utils.JWTUtils;
import com.csp.main.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author csp
 * @since 2022-06-05
 */
@RestController
@RequestMapping("/main/commodity")
@CrossOrigin
public class CommodityController {

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/list")
    public R list(){
        return R.ok().data("rows",commodityService.list(null));
    }

    @GetMapping("/list_by_user")
    public R listByUser(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);

        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Commodity> list = commodityService.list(wrapper);
        return R.ok().data("rows",list);
    }

    @GetMapping("/getById/{id}")
    public R getByID(@PathVariable String id){
        return R.ok().data("rows",commodityService.getById(id));
    }

    @PostMapping("/add")
    public R add(HttpServletRequest request,@RequestBody CommodityVo commodityVo){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);

        Commodity commodity = new Commodity();
        commodity.setTitle(commodityVo.getTitle());
        commodity.setPrice(commodityVo.getPrice());
        commodity.setKind(commodityVo.getKind());
        commodity.setUserId(userId);
        commodity.setContent(commodityVo.getContent());
        commodity.setImage(commodityVo.getImage());
        boolean save = commodityService.save(commodity);
        return save?R.ok():R.error();
    }

    @DeleteMapping("delete/{id}")
    public R deleteById(@PathVariable String id){
        boolean b = commodityService.removeById(id);
        return b?R.ok():R.error();
    }

    @PutMapping("/update")
    public R update(@RequestBody Commodity commodity){
        QueryWrapper<Commodity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",commodity.getId());
        boolean update = commodityService.update(commodity, wrapper);
        return update?R.ok():R.error();
    }

    @GetMapping("/search/{key}")
    public R search(@PathVariable String key){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",key).or().like("content",key);
        List<Commodity> list = commodityService.list(queryWrapper);
        return R.ok().data("rows",list);
    }

    @GetMapping("/his_add/{comId}")
    public R hisAdd(HttpServletRequest request,@PathVariable String comId){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        redisUtil.lSet(userId,comId,60*24*7);
        return R.ok();
    }

    @GetMapping("/his_list")
    public R hisList(HttpServletRequest request){
        String token = request.getHeader(JWTUtils.USER_LOGIN_TOKEN);
        String userId = JWTUtils.validateToken(token);
        List<Object> list = redisUtil.lGet(userId,0,-1);
        return R.ok().data("rows",list);
    }

    @GetMapping("/get_by_user_id/{userId}")
    public R getByUserId(@PathVariable String userId){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Commodity> list = commodityService.list(queryWrapper);
        return R.ok().data("rows",list);
    }

}

